﻿using System;
using Csla;
using Csla.Serialization;
using BizControl.DalLinq;
using BizControl.Library.Helpers;

namespace BizControl.Library.BusinessObjects.Marks
{
  [Serializable()]
  public class MarkInfo : ReadOnlyBase<MarkInfo>, ISupportEmptyValue
  {
    private readonly int ShortNameLength = 30;
    public Guid Id { get; private set; }
    public string Name { get; private set; }
    public MarkType Type { get; private set; }
    public MarkState State { get; private set; }
    
    public string VeryShortName
    {
      get { return StringHelper.GetShortenDescription(Name, ShortNameLength); }
    }

    public override string ToString()
    {
      return Name;
    }

    private MarkInfo()
    { /* require use of factory methods */ }

    internal MarkInfo(Guid id, string name, MarkType type, MarkState state)
    {
      Id = id;
      Name = name;
      Type = type;
      State = state;
    }

    internal MarkInfo(Guid id, string name, string tag, bool isActive)
    {
      Id = id;
      Name = name;
      Type = tag == ActivityMarkBO.ActivityMarkTagName ? MarkType.ActivityMark : MarkType.PersonMark;
      State = isActive ? MarkState.Completed : MarkState.NotCompleted;
    }

    internal MarkInfo(Mark mark, MarkType type)
    {
      if (mark != null)
      {
        Id = mark.MarkId;
        Name = mark.Name;
        Type = type;
        State = mark.IsActive ? MarkState.Completed : MarkState.NotCompleted;
      }
    }

    internal MarkInfo(PersonMarkBO mark)
    {
      if (mark != null)
      {
        Id = mark.MarkId;
        Name = mark.Name;
        Type = MarkType.PersonMark;
        State = mark.IsActive ? MarkState.Completed : MarkState.NotCompleted;
      }
    }

    internal MarkInfo(ActivityMarkBO mark)
    {
      if (mark != null)
      {
        Id = mark.MarkId;
        Name = mark.Name;
        Type = MarkType.ActivityMark;
        State = mark.IsActive ? MarkState.Completed : MarkState.NotCompleted;
      }
    }

    internal MarkInfo(MarkInfo mark)
    {
      if (mark != null)
      {
        Id = mark.Id;
        Name = mark.Name;
        Type = mark.Type;
        State = mark.State;
      }
    }

    internal static MarkType GetMarkType(Mark mark)
    {
      if (mark == null) return MarkType.UnknownMark;
      if (mark.ActivityRef != null) return MarkType.ActivityMark;
      if (mark.MarkPeople.Count > 0) return MarkType.PersonMark;
      return MarkType.UnknownMark;
    }

    public override bool Equals(object obj)
    {
      if (obj is MarkInfo)
        return this.Id == (obj as MarkInfo).Id;
      return base.Equals(obj);
    }

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    #region ISupportEmtpyValue Members

    public bool IsEmpty()
    {
      return Id == Guid.Empty;
    }

    #endregion
  }
}
